package com.fl.nacos.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@Slf4j
public class SentinelController {

    @GetMapping(value = "/testA")
    public String TestA(){
        int age = 10/0;
        return "----testA";
    }

    @GetMapping(value = "/testB")
    public String TestB(){
        log.info(Thread.currentThread().getName()+"\t"+new Date().toString()+ "----testB");
        return Thread.currentThread().getName()+"\t"+new Date().toString()+ "----testB";

    }
    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey")
    public String testHotKey(@RequestParam(value = "p1",required = false) String p1,
                             @RequestParam(value = "p2",required = false) String p2)
    {
//        int age = 10/0;
        return "------testHotKey";
    }
    //该方法为testHotKey兜底，只在sentinel配置异常时生效，java运行异常不生效
    public String deal_testHotKey (String p1, String p2, BlockException exception)
    {
        return "------deal_testHotKey,o(╥﹏╥)o";  //sentinel系统默认的提示：Blocked by Sentinel (flow limiting)
    }
}
